//请求截图，用找图方法，第一个就要先写的请求截图代码，不然找图没法用
if (!requestScreenCapture()) {
  toast("请求截图失败");
  exit();
}

let num = 1; //刷了多少次
//魂十一 色点
let hun11 = [
  "#9b9384",
  [
    [15, -6, "#7d766a"],
    [26, -4, "#e5d8c3"],
    [38, -4, "#e0d4bf"],
    [46, -4, "#e1d6bf"],
    [51, -3, "#a09888"],
    [51, 13, "#a29a89"],
    [43, 19, "#423e37"],
    [28, 17, "#e1d7c1"],
    [17, 16, "#d4c9b5"],
    [12, 16, "#5c574e"],
    [11, 4, "#e5dbc5"],
  ],
  { region: [3430, 1855, 87, 51], threshold: [26] },
];
//是否进入挑战
let is_tiaozhan = [
  "#57392b",
  [
    [14, 3, "#312b30"],
    [25, 6, "#4a3e3e"],
    [36, 5, "#dbb78f"],
    [37, 16, "#96806d"],
    [36, 26, "#3c3639"],
    [36, 39, "#423f3d"],
    [34, 51, "#b89a7b"],
    [20, 52, "#3d3739"],
    [8, 49, "#3a3334"],
    [-2, 49, "#6d4e36"],
    [-4, 49, "#835c41"],
    [-8, 34, "#866041"],
    [16, 31, "#3b3436"],
  ],
  { region: [28, 58, 72, 75], threshold: [26] },
];

//结束挑战的色点
let stop_tiaozhan = [
  "#0e0101",
  [
    [24, 4, "#6f3510"],
    [49, 3, "#0c0000"],
    [54, 28, "#75140d"],
    [28, 31, "#5b0b09"],
    [7, 33, "#5d0b09"],
    [5, 51, "#18000a"],
    [42, 54, "#1b0007"],
    [57, 52, "#1b0106"],
  ],
  { region: [1823, 1737, 71, 59], threshold: [26] },
];

//启动脚本
init();

function init() {
  start_tiaozhan();
}

//开始挑战
function start_tiaozhan() {
  console.log("开始挑战御魂脚本", num);
  getColor(hun11, true);

  let is_zhanDou = getColor(is_tiaozhan);
  if (is_zhanDou) {
    stop();
  } else {
    sleep(3000); //等待5秒
    start_tiaozhan();
  }
}

//挑战御魂完毕
function stop() {
  getColor(stop_tiaozhan, true);
  let res = getColor(hun11);
  if (res) {
    num++;
    start_tiaozhan();
  } else {
    click(1500, 1500);
    sleep(3000); //等待5秒
    stop();
  }
}

/*
 * 功能：多点找色
 * colorsArr 多点颜色信息
 * isClick 是否点击
 */
function getColor(colorsArr, isClick) {
  let res = false;
  try {
    let img = images.captureScreen(); //截屏
    sleep(10); //延迟一下，截图太快会卡顿
    let p = images.findMultiColors(
      img,
      colorsArr[0],
      colorsArr[1],
      colorsArr[2]
    );
    if (p) {
      if (isClick) {
        click(p.x, p.y);
        sleep(3000); //等待3秒
      }
      res = p;
    }
    img.recycle(); //释放内存
  } catch (err) {
    console.error(err);
  }

  return res;
}
